package com.jlu155.CourseManager.controller;

import com.jlu155.CourseManager.Bean.LoginRequest;
import com.jlu155.CourseManager.Bean.LoginResult;
import com.jlu155.CourseManager.Bean.PageRequest;
import com.jlu155.CourseManager.Bean.PageResult;
import com.jlu155.CourseManager.entity.Course;
import com.jlu155.CourseManager.entity.Message;
import com.jlu155.CourseManager.entity.Student;
import com.jlu155.CourseManager.entity.Teacher;
import com.jlu155.CourseManager.repository.TeacherRepository;
import com.jlu155.CourseManager.service.TeachClassService;
import com.jlu155.CourseManager.service.TeacherService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class TeacherController {

    @Resource
    private TeacherService teacherService;
    @Autowired
    private TeacherRepository teacherRepository;
    @RequestMapping("/teacherLogin")
    public ResponseEntity login(@RequestBody LoginRequest loginInfo, HttpSession session){
        boolean valid=teacherService.loginValidate(loginInfo.getId(),loginInfo.getPasswd());
        if(valid) {
            Teacher teacher=teacherRepository.findTeacherById(loginInfo.getId());
            session.setAttribute("teacher_id", loginInfo.getId());
            session.setAttribute("teacher_type",teacher.getType());
            return ResponseEntity.ok().body(teacher.getType()==1?"admin":"teacher");
        }
        else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
        }
    }

    @PostMapping("/insertTeacher")
    public ResponseEntity insertTeacher(@RequestBody Teacher teacher, HttpSession session){
        Integer teacher_type=(Integer)session.getAttribute("teacher_type");
        if(teacher_type!=1)
            return ResponseEntity.badRequest().body("权限不足");
        if(teacherRepository.findTeacherById(teacher.getTeacher_id())!=null)
            return ResponseEntity.badRequest().body("错误，课程已存在");
        teacherRepository.insertTeacher(teacher);
        return ResponseEntity.ok().body("增加课程成功");
    }
    @PostMapping("/updateTeacher")
    public ResponseEntity updateTeacher(@RequestBody Teacher teacher,HttpSession session){
        Integer teacher_type=(Integer)session.getAttribute("teacher_type");
        if(teacher_type!=1)
            return ResponseEntity.badRequest().body("权限不足");
        if(teacherRepository.findTeacherById(teacher.getTeacher_id())==null)
            return ResponseEntity.badRequest().body("错误，课程不存在");
        teacherRepository.updateTeacher(teacher);
        return ResponseEntity.ok().body("修改课程成功");
    }

    @PostMapping("/deleteTeacher")
    public ResponseEntity deleteTeacher(@RequestBody Integer tid){
        try{
            teacherRepository.deleteTeacher(tid);
            return ResponseEntity.ok(true);
        } catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    @PostMapping("/getTeachers")
    public PageResult<Teacher> getTeachers(@RequestBody PageRequest pageRequest){

        return teacherService.getTeacherPage(pageRequest);
    }

    @RequestMapping("/updateTeacherPasswd")
    public ResponseEntity updateTeacherPasswd (@RequestBody String passwd,HttpSession session){
        Integer tid= (Integer) session.getAttribute("teacher_id");
        if(tid==null){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("登录无效");
        }
        try{
            teacherRepository.updateTeacherPasswdById(tid,passwd);
            return ResponseEntity.ok().body(null);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body(e.getMessage());
        }
    }

}
